/**
 * 
 */
package com.collabscm.md.base.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.collabscm.md.base.dao.OrganDao;
import com.collabscm.md.base.model.Organ;
import com.collabscm.pf.dao.support.DomainDao;
import com.collabscm.pf.logging.Logger;
import com.collabscm.pf.logging.LoggerFactory;

/**
 * @author tony
 * 组织处理相关的DAO实现。
 */
@Repository
public class OrganDaoImpl extends DomainDao<Organ, Long> implements OrganDao<Organ, Long> {
	/**
	 * 日志对象
	 */
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 取出所有组织
	 * @return List <Organ>
	 */
	@Override
	public List <Organ> getAllOrgan(int start, int pageSize) {
		Query query = entityManager.createNamedQuery(Organ.GET_ALL_ORGAN);
		query.setParameter("status", 2001);//TODO
		query.setFirstResult(start);
		query.setMaxResults(pageSize);
		List<Organ> organList = query.getResultList();
		logger.info("返回 organList 大小为 : {}", organList.size());
		if (organList.isEmpty()) {
			logger.debug("系统没有组织!");
			return null;
		} else {
			return organList;
		}
	}

	/**
	 * 得到组织总数
	 * @return 总数
	 */
	@Override
	public Integer getCountOrgan() {
		Query query = entityManager.createNamedQuery(Organ.COUNT_ALL_ORGAN);
		query.setParameter("status", 2001);//TODO
		return new Integer(query.getSingleResult().toString());
		//return new Integer(this.findNamedQueryCount(Organ.COUNT_ALL_ORGAN + CommonConstants.STATUS_ENABLE) + "");
	}
	
	/**
	 * 创建组织
	 * @param organ
	 * @return
	 */
	public Long createOrgan(Organ organ) {
		//entityManager.getTransaction().begin();
		entityManager.persist(organ);
		//entityManager.getTransaction().commit();
		return null;
	}
	
	/**
	 * 得到organId
	 * @return 
	 */
	@Override
	public Long getOrganId() {
		return (Long) entityManager.createNamedQuery(Organ.MAX_ID).getSingleResult() + 1;
	}

}
